﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Vintner.Sales.ModuleData
{
    internal class RadarSaleInvoicesParam : RadarParameters
    {
        public RadarSaleInvoicesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar RadarSaleInvoices

    internal class RadarSaleInvoices : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3;

        public bool isDado { get; set; }

        public RadarSaleInvoices()
            : base()
        {
            rdrCodeColumn = VI_SaleInvoices.InvoiceNo.Name;
            rdrDescColumn = VI_SaleInvoices.Description.Name;
        }

        protected override Table GetRadarTable()
        {
            return new VI_SaleInvoices();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", VI_SaleInvoices.IsDado);
            p2 = new SqlABParameter("@p2", VI_SaleInvoices.InvoiceNo);

            qb.Clear();
            qb.AddSelectAll(VI_SaleInvoices.Name);
            qb.AddFrom(VI_SaleInvoices.Name);
            qb.AddCompare(VI_SaleInvoices.IsDado, p1);
            qb.AddCompare(VI_SaleInvoices.InvoiceNo, p2);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = isDado;
            p2.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[VI_SaleInvoices.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", VI_SaleInvoices.IsDado);

            qb.Clear();
            qb.AddSelectAll(VI_SaleInvoices.Name);
            qb.AddFrom(VI_SaleInvoices.Name);
            qb.AddCompare(VI_SaleInvoices.IsDado, p3);
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = isDado;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string saleNo = DataGridViewRowValues.GetValue<string>(row, VI_SaleInvoices.InvoiceNo);
            return GetRadarParameters(saleNo);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarSaleInvoicesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion
}